package com.ltmall.api.comm;

import com.ltmall.api.bean.AreaPostage;
import com.ltmall.api.bean.LogisticPatternExc;
import com.ltmall.api.entity.PageLogisticPatternExc;
import com.ltmall.api.entity.PageNationPostage;
import com.ltmall.api.entity.PagePostage;

import java.util.ArrayList;
import java.util.List;

public class LogiPatternParse {

    private static List<PageLogisticPatternExc> excList;

    public static List<PageLogisticPatternExc> parsePatternExc(List<LogisticPatternExc> list) {
//		excList.clear();//清除数据
        excList = new ArrayList<PageLogisticPatternExc>();
        for (LogisticPatternExc exc : list) {
            float initKG = exc.getInitKG();
            float initFee = exc.getInitFee();
            float addKG = exc.getAddKG();
            float addFee = exc.getAddFee();

            String zone = exc.getZone();
            String province = exc.getProvince();
            String city = exc.getCity();


            if (patternExcIn(initKG, initFee, addKG, addFee)) {// 表明这样的状态有了，把地址加进去
                String address = getPatternAddress(initKG, initFee, addKG, addFee);
                String partAddress = getAddress(zone, province, city);
                address += ("," + partAddress);
                setPatternAddress(initKG, initFee, addKG, addFee, address);
            } else {
                PageLogisticPatternExc pExc = new PageLogisticPatternExc();
                pExc.setInitKG(initKG);
                pExc.setInitFee(initFee);
                pExc.setAddKG(addKG);
                pExc.setAddFee(addFee);

                String address = getAddress(zone, province, city);

                pExc.setAddress(address);
                excList.add(pExc);
            }
        }

        return excList;
    }

    // 先判断数组里面有没有
    public static boolean patternExcIn(float initKG, float initFee, float addKG,
                                       float addFee) {
        for (PageLogisticPatternExc exc : excList) {
            if (exc.getInitKG() == initKG && exc.getInitFee() == initFee
                    && exc.getAddKG() == addKG && exc.getAddFee() == addFee) {
                return true;
            }
        }
        return false;
    }

    // 获取地址
    public static String getPatternAddress(float initKG, float initFee, float addKG,
                                           float addFee) {
        for (PageLogisticPatternExc exc : excList) {
            if (exc.getInitKG() == initKG && exc.getInitFee() == initFee
                    && exc.getAddKG() == addKG && exc.getAddFee() == addFee) {
                return exc.getAddress();
            }
        }
        return null;
    }

    //设置地址
    public static void setPatternAddress(float initKG, float initFee, float addKG,
                                         float addFee, String address) {
        for (PageLogisticPatternExc exc : excList) {
            if (exc.getInitKG() == initKG && exc.getInitFee() == initFee
                    && exc.getAddKG() == addKG && exc.getAddFee() == addFee) {
                exc.setAddress(address);
            }
        }
    }


    //获取唯一的地址
    public static String getAddress(String zone, String province, String city) {
        if (province != null) {
            return province;
        } else {
            return city;
        }
    }

    //全国包邮的地址
    public static PageNationPostage getNationPostage(List<AreaPostage> apList) {
        PageNationPostage pp = new PageNationPostage();
        pp.setNation("全国");

        String noArea = "";
        for (AreaPostage ap : apList) {
            if (ap.getNoProvince() == null && ap.getNoCity() == null) {
                pp.setPostage(ap.getPostage() + "");
                pp.setInitKG(ap.getInitKG());
                pp.setAddKG(ap.getAddKG());
                pp.setAddFee(ap.getAddFee());
            } else {
                if (ap.getNoCity() != null) {
                    noArea += ("," + ap.getNoCity());
                } else {
                    noArea += ("," + ap.getNoProvince());
                }
            }
        }
        if (!noArea.equals("")) {
            noArea = noArea.substring(1);
        }
        pp.setExcArea(noArea);

        return pp;
    }

    // 包邮的地址
    public static List<PagePostage> postageList(List<AreaPostage> apList) {
        List<PagePostage> list = new ArrayList<PagePostage>();
        for (AreaPostage ap : apList) {
            PagePostage page = new PagePostage();
            page.setPostage(ap.getPostage() + "");
            page.setInitKG(ap.getInitKG());
            page.setAddKG(ap.getAddKG());
            page.setAddFee(ap.getAddFee());
            if (ap.getProvince() != null) {
                page.setArea(ap.getProvince());
            } else {
                page.setArea(ap.getCity());
            }
            list.add(page);
        }
        return list;
    }


}